$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
layui.use(['form','laydate', 'laypage', 'layer', 'table', 'upload', 'element','tree'], function() {
	var form = layui.form
	  ,laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  ,layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作	
	  var path=getRootPath();
			//监听表格复选框选择
			table.on('checkbox(demo)', function(obj) {
				console.log(obj)
			});
			//上传文件
			  var uploadInst = upload.render({
			      elem: '#upload1' //绑定元素
			      ,url: path+'upload/uploadFile.action' //上传接口
			      //,url: 'http://43.227.66.156/upload/uploadFile' //上传接口
			      ,before: function(obj){
			          //预读本地文件示例，不支持ie8
			          obj.preview(function(index, file, result){
			        	  
			              $('#demoOne').attr('src', result); //图片链接（base64）
			          });
			      }
			      ,done: function(res){
			          //如果上传失败
			          if(res.code > 0){
			              return layer.msg('上传失败');
			          }
			          //上传成功
			          document.getElementById("img_url").value = res.url;

			      }
			      ,error: function(){
			          //演示失败状态，并实现重传
			          var demoText = $('#demoText');
			          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
			          demoText.find('.demo-reload').on('click', function(){
			              uploadInst.upload();
			          });
			      }
			  });
			  //上传文件
			  var uploadInsts = upload.render({
			      elem: '#uploadTwo' //绑定元素
			      ,url: path+'upload/uploadFile.action' //上传接口
			      ,before: function(obj){
			          //预读本地文件示例，不支持ie8
			          obj.preview(function(index, file, result){
			        	  
			              $('#demoTow').attr('src', result); //图片链接（base64）
			          });
			      }
			      ,done: function(res){
			          //如果上传失败
			          if(res.code > 0){
			              return layer.msg('上传失败');
			          }
			          //上传成功
			          document.getElementById("img_urlTwo").value = res.url;

			      }
			      ,error: function(){
			          //演示失败状态，并实现重传
			          var demoText = $('#demoTextTwo');
			          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
			          demoText.find('.demo-reload').on('click', function(){
			        	  uploadInsts.upload();
			          });
			      }
			  });
			//监听工具条
			table.on('tool(demo)', function(obj) {
				var data = obj.data;
				if (obj.event === 'del') {
					layer.confirm('确定删除该用户吗？', function(index) {
						layer.close(index);
						var id = data.manId;
						var url = "../../drugManager/delete.action";
						var datas = {
							'manId' : id
						};
						$.ajax({
							asyn : true,
							type : "POST",
							url : url,
							data : datas,
							success : function(msg) {
								if (msg.num == "1") {
									layer.msg(msg.msg);
									//table.reload('idTest');
		                           location.reload();
								} else {
									layer.msg(msg.msg);
								}
							},
							error : function(fh) {
								layer.msg(msg.msg);
							}
						},"json");
					});
				} else if (obj.event === 'edit') {
					$('#updateReload').css('display','block');
					$("#manId").val(data.manId);
					$("#manUser").val(data.manUser);
					$("#manPwd").val(data.manPwd);
					$("#manPhone").val(data.manPhone);
					$("#manCardid").val(data.manCardid);
					$("#img_urlTwo").val(data.manRemark);
					$("#demoTow").attr("src",path+data.manRemark); 
				    $("#isva").find("option[value="+data.isva+"]").attr("selected","selected");
				    
					
					$.ajax({
						asyn : false,
						type : "POST",
						url : "../../filiale/findAllFiliale.action",
						data:{"filId":data.filId},
						success : function(datas) {
							$("#filIdss").html("");
							for(var i=0;i<datas.length;i++){
								if(data.filId==datas[i].filId){
									$("#filIdss").append("<option selected value="+datas[i].filId+">"+datas[i].filName+"</option>");
								}else{
									$("#filIdss").append("<option value="+datas[i].filId+">"+datas[i].filName+"</option>");
								}
								
							}
							layui.form.render();
						},
						error : function(fh) {
						}
					});
					
				    $.ajax({
						asyn : true,
						type : "POST",
						url : "../../drugManager/findRole.action",
						data:{"filId":data.filId},
						success : function(msg) {
							
							var htmlStr = '';
							for(var i=0;i<msg.length;i++){
								
								htmlStr+='<input type="checkbox" name="role" value="'+msg[i].roleId+'" title="'+msg[i].roleName+'" >';
							}
							$("#roles").html(htmlStr);
							$.post("../../drugManager/findRoleByManId.action",{"manId":data.manId},function(usRole){
								for(var i=0;i<usRole.length;i++){
									$("input[name='role']").each(function(){
			                            if($(this).val()==usRole[i].roleId){
			                                $(this).prop('checked',true); 
			                            }
			                        });
									layui.form.render();
								}
							},"json");
							$("#update").click(function(){
								var str = "";
		                        $("input[name='role']").each(function(){    
		                            if($(this).is(":checked")){    
		                                str += $(this).val()+"-";    
		                            }    
		                        });
		                        $("#roleIds").val(str);
								$("#updateMess").submit();
							});
							layui.form.render();
						},
						error : function(fh) {
						}
					});
					
				} else if (obj.event === 'vet') {
					layer.confirm('确定审核该用户吗？', function(index) {
						layer.close(index);
						$.ajax({
							asyn : true,
							type : "POST",
							url : "../../drugManager/updateVet.action",
							data:{"manId":data.manId,"manUser":data.manUser,"usersPwd":data.usersPwd},
							success : function(msg) {
								table.reload('idTest');
							},
							error : function(fh) {
							}
						});
					});
				}
			});
			$("#query").on("click",function(){
				//执行重载
				table.reload('idTest', {
					where : {
						'manUser' : $("#name").val(),
						'manPhone' : $("#phone").val()
					},
					page : {
						curr : 1
					}
				});
			});
			$('#closeAdd').on('click', function() {
				$('#addReload').css('display', 'none');
			});
			$('#closeUpdate').on('click', function() {
				
				$('#updateReload').css('display', 'none');
			});
			
			form.on('submit(drugTypeAdd)',function(){
				  parent.location.reload(); // 父页面刷新
			  });
		});
		function add() {
			$('#addReload').css('display', 'block');
			var url="../../filiale/findQxFiliale.action";
			var data=null;
			$("#filIds").html("");
			$.post(url,data,function(data){
				for(var i=0;i<data.length;i++){
					$("#filIds").append("<option value="+data[i].filId+">"+data[i].filName+"</option>");
				}
				layui.form.render();
			});
			$.ajax({
				asyn : true,
				type : "POST",
				url : "../../drugManager/findRole.action",
				success : function(msg) {
					var htmlStr = '';
					
					for(var i=0;i<msg.length;i++){
						htmlStr+='<input type="checkbox" name="role" value="'+msg[i].roleId+'" title="'+msg[i].roleName+'" >';
					}
					$("#Addroles").html(htmlStr);
					$("#add").click(function(){
						var str = "";    
                        $("input[name='role']").each(function(){    
                            if($(this).is(":checked")){    
                                str += $(this).val()+"-";    
                            }    
                        });
                        $("#addRoleIds").val(str);
                        
					});
					layui.form.render();
				},
				error : function(fh) {
				}
			});
			
		}
		

function hoverOpenImg(){
    var img_show = null; // tips提示
    $('td img').hover(function(){
        var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:130px;' />";
        img_show = layer.tips(img, this,{
            tips:[2, 'rgba(41,41,41,.5)']
            ,area: ['160px']
        });
    },function(){
        layer.close(img_show);
    });
    $('td img').attr('style','max-width:70px');
}
